#include "modbuswig.h"
#include "ui_modbuswig.h"

ModbusWig::ModbusWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModbusWig)
{
    ui->setupUi(this);
    group = new QButtonGroup(this);
    group->addButton(ui->pushButton_2, 1);
    connect(group, SIGNAL(buttonClicked(int)), this, SLOT(toggledSlot(int)));
}

ModbusWig::~ModbusWig()
{
    delete ui;
}

void ModbusWig::paintEvent(QPaintEvent *event)
{
    // 创建画家类对象
        // 参数为 QPaintDevice *表示可绘制的对象
        QPainter painter(this);

        QPixmap map(":/new/prefix1/beijing.png");
        // 绘制图片
        painter.drawPixmap(0,0,this->width(),this->height(),map);
}
void ModbusWig::toggledSlot(int a)
{
   if(a==1)
   { QJsonObject pageObject;

       GlobalVal::mqtt->publish(glo.mqtt->message1);
   }
}
void ModbusWig::dataUpSlot(QByteArray message)
{    ui->mod300->clear();
     ui->mod301->clear();
      ui->mod302->clear();
       ui->mod303->clear();
        ui->mod304->clear();
         ui->mod305->clear();
    QJsonParseError jsonError;
    QJsonDocument ob = QJsonDocument::fromJson(message, &jsonError);
    if (!ob.isNull() && (jsonError.error == QJsonParseError::NoError))
    {
        if (ob.isObject())
        {
            QJsonObject obj = ob.object();
            if (obj.contains("modbus"))
            {
                 QJsonValue value  = obj.value("modbus");
                   {

                       {
                           {
                               if (value.isObject())
                               {
                                   QJsonObject subobj = value.toObject();

                                   {

                                       {
                                           if (subobj.contains("weight"))
                                           {
                                               QJsonValue subValue = subobj.value("weight");
                                               double ArrayKey = subValue.toDouble();
                                               QString str = QString::number(ArrayKey);
                                               qDebug() << "weight : " << ArrayKey;
                                                ui->mod300->append(str);
                                           }
                                       }

                                       {
                                           if (subobj.contains("tem"))
                                           {
                                               QJsonValue subValue = subobj.value("tem");
                                               double ArrayKey = subValue.toDouble();
                                               QString str = QString::number(ArrayKey);
                                               qDebug() << "tem: " << ArrayKey;
                                               ui->mod301->append(str);
                                           }
                                       }

                                       {
                                           if (subobj.contains("in_pre"))
                                           {
                                               QJsonValue subValue = subobj.value("in_pre");
                                               double ArrayKey = subValue.toDouble();
                                               QString str = QString::number(ArrayKey);

                                               qDebug() << "in_pre : " << ArrayKey ;
                                                ui->mod302->append(str);

                                           }
                                       }

                                       {
                                           if (subobj.contains("in_h2"))
                                           {
                                               QJsonValue subValue = subobj.value("in_h2");
                                               double ArrayKey = subValue.toDouble();
                                               QString str = QString::number(ArrayKey);
                                               qDebug() << "in_h2 : " << ArrayKey;
                                                ui->mod303->append(str);
                                           }
                                       }

                                       {
                                           if (subobj.contains("out_h2"))
                                           {  subobj.value("out_h2")=false;
                                               QJsonValue subValue = subobj.value("out_h2");
                                               double ArrayKey = subValue.toDouble();
                                               QString str = QString::number(ArrayKey);
                                                qDebug() << "out_h2 : " <<ArrayKey;
                                                 ui->mod304->append(str);
                                           }
                                       }

                                       {
                                           if (subobj.contains("air_pre"))
                                           {
                                               QJsonValue subValue = subobj.value("air_pre");
                                               double ArrayKey = subValue.toDouble();
                                               QString str = QString::number(ArrayKey);
                                               qDebug() << "air_pre : " << ArrayKey;
                                                ui->mod305->append(str);
                                           }

                                       }
                                   }

                               }
                           }
                       }
                   }
               }}
    }
}
